package org.example.server.search.config;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.apache.http.impl.nio.reactor.IOReactorConfig;
import org.elasticsearch.client.Node;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@Slf4j
public class ElasticSearchConfig {  
  
    @Bean
    public RestClient esClient() {
        RestClientBuilder builder = RestClient.builder(
                        new HttpHost("localhost", 9200))
                .setFailureListener(new RestClient.FailureListener(){
                    @Override
                    public void onFailure(Node node) {
                        log.error("ES失败回调. node={}",node);
                    }
                })
                .setHttpClientConfigCallback(httpAsyncClientBuilder -> httpAsyncClientBuilder.setDefaultIOReactorConfig(
                        IOReactorConfig.custom()
                                .setIoThreadCount(Runtime.getRuntime().availableProcessors())
                                .build()))
                .setRequestConfigCallback(
                        requestConfigBuilder -> requestConfigBuilder
                                .setAuthenticationEnabled(false)
                                .setConnectTimeout(5000)
                                .setSocketTimeout(60000));
        return builder.build();
    }  
  
}